// pages/searchPatrol/searchPatrol.js
const lbbService = require('../service/service.js')
const util = require('../../utils/util.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNum: 4,
    membersName: ['选择责任人查询'],
    manIndex: 0,
    masterUid: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMembers()
    this.getNowDate()
    this.getList()
    this.setData({
      scrollHeight: app.getScrollHeight(110)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getNowDate: function () {
    let that = this
    let date = new Date()
    let year = date.getFullYear()
    // 月份数组
    let monthsArray = [year + '-01', year + '-02', year + '-03', year + '-04', year + '-05', year + '-06', year + '-07', year + '-08', year + '-09', year + '-10', year + '-11', year + '-12']
    let nearThree = []
    let threeText = []
    let month = date.getMonth()
    nearThree.push(monthsArray[month])
    threeText.push(month + 1 + '月巡检')
    for (let i = 1; i <= 2; i++) {
      date.setMonth(month - i)
      nearThree.unshift(monthsArray[date.getMonth()])
      threeText.unshift(date.getMonth() + 1 + '月巡检')
    }
    console.log('>>', nearThree, threeText)
    that.setData({
      nearThree: nearThree,
      threeText: threeText
    })
  },
  toggleTab: function(e){
    let that = this
    let num = e.currentTarget.dataset.num
    that.setData({
      tabNum:num
    })
    that.setList()
  },
  getList: function(){
    let that = this
    lbbService.searchPatrol({ userId: that.data.masterUid },function(res){
      that.setData({
        patrols:res.data
      })
      that.setList()
    })
  },
  toPileDetail: function (e) {
    console.log("输出了e", e)
    let reportId = e.currentTarget.dataset.id
    console.log("输出了reportId", reportId)
    if (reportId==0){
        return;
    }else{
      wx.navigateTo({
        url: '/pages/patrolReport/patrolReport?reportId=' + reportId
      })
    }
  },
  setList: function(){
    let that = this
    let num = that.data.tabNum
    let patrols = that.data.patrols
    if(!patrols){
      that.setData({
        patrolList: []
      })
      return;
    }
    let patrolList = []
    if (num==1){
      patrolList = patrols.noInspectionInfoMap.noInspectionList
    } else if (num==2){
      patrolList = patrols.isInspectionInfoMap.isInspectionList.filter(item => item.inspectionEndTime.indexOf(that.data.nearThree[0]) !=-1)
    } else if (num == 3) {
      patrolList = patrols.isInspectionInfoMap.isInspectionList.filter(item => item.inspectionEndTime.indexOf(that.data.nearThree[1]) != -1)
    } else if (num == 4) {
      patrolList = patrols.isInspectionInfoMap.isInspectionList.filter(item => item.inspectionEndTime.indexOf(that.data.nearThree[2]) != -1)
    }
    that.setData({
      patrolList: patrolList.sort((a, b) => { return a.inspectionEndTime > b.inspectionEndTime?1:-1})
    })
  },
  getMembers: function () {
    let that = this
    lbbService.getMembers({}, function (res) {
      that.setData({
        members: res.data
      })
      that.setMembers()
    })
  },
  setMembers: function () {
    let that = this
    let members = that.data.members
    let membersName = that.data.membersName
    members.forEach(item => {
      membersName.push(item.masterName)
    })
    that.setData({
      membersName: membersName
    })
  },
  manChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      manIndex: index
    })
    if (that.data.manIndex > 0) {
      that.setData({
        masterUid: that.data.members[that.data.manIndex - 1].masterUid
      })
    } else {
      that.setData({
        masterUid: 0
      })
    }
    that.getList()
  },
})